package itsm.isperp.module.controller.app;

import itsm.isperp.framework.web.controller.TreeNodeController;
import itsm.isperp.framework.web.response.ResultMessage;
import itsm.isperp.module.entity.app.AppDict;
import itsm.isperp.module.entity.app.AppSettingConfig;
import itsm.isperp.module.service.app.AppDictService;
import itsm.isperp.module.service.app.AppSettingConfigService;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

/**
 * 系统设置配置的控制器
 * 
 * @author lizx
 * @date 2013-7-20
 * @version V1.0
 */
@Controller
@RequestMapping("app/settingConfig")
public class AppSettingConfigController extends
		TreeNodeController<AppSettingConfig> {

	@Autowired
	protected AppSettingConfigService appSettingConfigService;

	@Autowired
	protected AppDictService appDictService;

	@Override
	public AppSettingConfigService getService() {
		return this.appSettingConfigService;
	}

	@Override
	public String getPageTitle() {
		return "系统设置配置";
	}

	@RequestMapping("wizard/{code}")
	public ModelAndView wizard(@PathVariable("code") String code) {
		ModelAndView mv = new ModelAndView("app/settingConfig/wizard");

		List<AppSettingConfig> configs = this.getService().getChildrenByCode(
				code);
		mv.addObject("configs", configs);
		mv.addObject("code", code);

		AppSettingConfig root = this.getService().getByCode(code);

		mv.addObject("root", root);

		return mv;
	}

	@RequestMapping("settingEdit")
	public ModelAndView settingEdit(String code, String id) {
		ModelAndView mv = new ModelAndView("app/settingConfig/settingEdit");
		AppSettingConfig root = this.getService().getByCode(code);
		List<AppSettingConfig> configs = this.getService().getChildrenByCode(
				code);

		mv.addObject("configs", configs);
		mv.addObject("root", root);
		mv.addObject("currentConfigId", id);
		return mv;
	}

	@RequestMapping("getUrgencyAndEffect")
	@ResponseBody
	public ArrayList<ArrayList<String>> getUrgencyAndEffect(String urgencyCode,
			String effectCode) {
		ArrayList<String> jjd = new ArrayList<String>();
		ArrayList<String> yxd = new ArrayList<String>();
		List<AppDict> urgencyList = appDictService.findByType(urgencyCode,
				"code DESC");
		List<AppDict> effectList = appDictService.findByType(effectCode,
				"code DESC");
		for (AppDict d : urgencyList) {

			jjd.add(d.getCode());
		}
		for (AppDict d : effectList) {
			yxd.add(d.getCode());
		}

		ArrayList<ArrayList<String>> arr = new ArrayList<ArrayList<String>>();
		arr.add(jjd);
		arr.add(yxd);
		return arr;

	}

	@RequestMapping("prioritySetting")
	public ModelAndView prioritySetting(String urgencyCode, String effectCode,
			String priorityCode) {
		ModelAndView mv = new ModelAndView("app/settingConfig/prioritySetting");

		List<AppDict> priorityList = appDictService.findByType(priorityCode);

		mv.addObject("urgencyCode", urgencyCode);
		mv.addObject("effectCode", effectCode);
		String priorityColorStr = "";
		for (AppDict d : priorityList) {
			priorityColorStr += d.getFontColor() + ",";
		}
		if (StringUtils.isNotEmpty(priorityColorStr)) {
			mv.addObject("priorityColorStr", priorityColorStr.substring(0,
					priorityColorStr.length() - 1));
		}
		mv.addObject("priorityCode", priorityCode);

		int s = priorityList.size();

		mv.addObject("select" + s, "selected='seleceted'");

		return mv;
	}

	@RequestMapping("prioritySettingSave")
	@ResponseBody
	public ResultMessage prioritySetting(HttpServletRequest req) {

		String data = req.getParameter("data");

		String priorityCode = req.getParameter("priorityCode");

		String[] dataArr = data.split(",");

		appDictService.deleteByTypeCode(priorityCode);

		for (int i = 0; i < dataArr.length; i++) {
			String str = dataArr[i];
			String[] pArr = str.split(":");
			AppDict dict = new AppDict();
			// dict.setDescription("优先级")
			dict.setEnable(true);
			dict.setName(pArr[0]);
			dict.setCode(pArr[1]);
			dict.setOrderNum(i + 1);
			dict.setTypeCode(priorityCode);
			appDictService.saveOrUpdate(dict);
		}

		return ResultMessage.success();
	}
}
